package wawi.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class WawiLogger
{
	private static boolean inited = false;
	
	public static void init()
	{
		if (inited) return;
		else inited = false;
		
        //Logger konfigurieren      
        try
        {                        
            Properties p = new Properties();
            p.load(new FileInputStream("C:\\wawi\\log4j.properties"));            
            p.setProperty("log4j.appender.fileout.File", "C:\\wawi\\log\\"+makeLogDirName()+".log");
            PropertyConfigurator.configure(p);
        }
        catch (IOException e)
        {
            //Bei Fehler - Basiskonfiguration
            BasicConfigurator.configure();
        }
        Logger l = Logger.getLogger(WawiLogger.class);
        l.info("Logger cofigured");
	}
	
    private static String makeLogDirName()
    {
        GregorianCalendar c = new GregorianCalendar();
        
        int jahr = c.get(Calendar.YEAR);
        int monat = c.get(Calendar.MONTH)+1;
        int tag = c.get(Calendar.DAY_OF_MONTH);
        int h = c.get(Calendar.HOUR_OF_DAY);
        int min = c.get(Calendar.MINUTE);
        int sek = c.get(Calendar.SECOND);
        
        String ret = str(jahr)+"-"+str(monat)+"-"+str(tag)+"_"+str(h)+"-"+str(min)+"-"+str(sek);
        
        return ret;
    }
    
    private static String str (int f)
    {
        if (f<10)
            return "0"+f;
        else
            return ""+f;
    }
}
